// component/ganttChart/ganttChart.js
import {
  getDate,
  getLocaleString
} from '../../library/configGantt'
const sysInfo = wx.getSystemInfoSync()
const scaleRate = sysInfo.windowWidth / 750;
const platform = sysInfo.platform;
const ONE_DAY = 1000 * 60 * 60 * 24;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    width: {
      type: String,
      value: 750
    },
    totalWidth: {
      type: Number,
      value: 1500
    },
    gantt: {
      type: Object
    },
    height: {
      type: String
    },
    elementHeight: {
      type: String,
    },
    explainColor: {
      type: String,
      value: '#000000'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    ganttList: [],
    scrollX: 0,
    scrollY: 0,
    interval: 0,
    dateScroll: undefined,
    pointerX: 0,
    stablityScroll: 0,
    inited: false,
    start: undefined,
    end: undefined,
    users: undefined,
    missions: undefined,
    rulerWidth: 0,
    canAnimation: false,
    hasPointer: false,
    eHeight: 30,
  },
  lifetimes: {
    attached() {}
  },
  observers: {
    'elementHeight': function(eHeight) {
      this.setData({
        eHeight: +eHeight
      })
      this.checkUserDis();
    },
    'gantt': function(gantt) {
      if (!gantt) return;
      let stablityScroll;
      if (gantt.pointer.enable && !gantt.limitTimeRange && gantt.pointer.x > this.data.width / 2) {
        stablityScroll = (gantt.pointer.x - this.data.width / 2) * scaleRate;
      } else {
        stablityScroll = 0;
      }
      this.setData({
        ganttList: gantt.ganttList,
        start: gantt.start,
        end: gantt.end,
        totalWidth: gantt.totalWidth,
        interval: gantt.interval,
        hasPointer: gantt.pointer.enable,
        pointerX: gantt.pointer.x,
        stablityScroll,
        dateScroll: stablityScroll,
        inited: true,
      })
      this.checkUserDis();
      this.figureOutWeekends();
    },

  },
  /**
   * 组件的方法列表
   */
  methods: {
    onSelfScroll(e) {
      //检查组件是否显示在视图中
      if (e.detail.scrollLeft !== this.data.dateScroll) {
        //更新状态
        this.setData({
          dateScroll: e.detail.scrollLeft,
        })
        this.checkUserDis();

      }
      this.setData({
        scrollY: e.detail.scrollTop
      })
    },
    checkUserDis() {
      const range = {
        left: this.data.dateScroll / scaleRate,
        right: this.data.dateScroll / scaleRate + this.data.width
      }
      let userDisList = [];
      for (let i = 0; i < this.data.ganttList.length; i++) {
        const gantt = this.data.ganttList[i];
        for (const item of gantt) {
          //确定该条形图在视图中
          if (
            (item.left >= range.left && item.left <= range.right) ||
            (item.right >= range.left && item.right <= range.right) || (item.left <= range.left && item.right >= range.right)) {
            //在视图中显示状态指示器和用户
            //计算指示器位置
            let left = item.left - range.left;
            if (left < 0) {
              if (item.right - range.left > 40) left = 40;
              else left = item.right - range.left;
            } else left += 40;
            userDisList.push({
              id: item.id,
              top: i * (this.data.eHeight + 1),
              left,
              target: item
            })
          }
        }
      }
      this.setData({
        userDisList
      })
    },
    figureOutWeekends() {
      let {
        start,
        end
      } = this.data;
      const weekends = [];
      start = getDate(start);
      end = getDate(end);
      for (let i = start.getTime(), j = 0; i < end.getTime(); i += ONE_DAY, j++) {
        const tempDate = new Date(i);
        if (tempDate.getDay() == 6) {
          weekends.push({
            left: j * this.data.interval,
            width: 2 * this.data.interval
          })
          // weekends.push(j++ * this.data.interval)
          i += ONE_DAY * 6;
          j += 6;
        } else if (tempDate.getDay() == 0) {
          weekends.push({
            left: j * this.data.interval,
            width: 1 * this.data.interval
          })
          i += ONE_DAY * 5;
          j += 5
        }
      }
      this.setData({
        weekends
      });
    },

  }
})